package com.sub.project.info.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Arrays;

import com.sub.project.info.domain.UserEducational;
import com.sub.project.info.service.ITbSchoolService;
import com.sub.project.info.service.IUserEducationalService;
import com.sub.project.info.vo.EducationSchoolVo;
import com.sub.project.info.vo.RestVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sub.framework.aspectj.lang.annotation.Log;
import com.sub.framework.aspectj.lang.enums.BusinessType;
import com.sub.framework.web.controller.BaseController;
import com.sub.framework.web.domain.AjaxResult;
import com.sub.common.utils.poi.ExcelUtil;
import com.sub.framework.web.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2020-01-07
 */
@RestController
@RequestMapping("/system/educational" )
public class UserEducationalController extends BaseController {
    @Autowired
    private IUserEducationalService userEducationalService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:educational:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(UserEducational userEducational) {
        startPage();
        QueryWrapper<UserEducational> queryWrapper = new QueryWrapper<UserEducational>();
if(userEducational.getStarttime() != null){
     queryWrapper.eq("startTime" ,userEducational.getStarttime());
}
    if(userEducational.getEndtime() != null){
     queryWrapper.eq("endTime" ,userEducational.getEndtime());
}
    if(userEducational.getSchoolid() != null){
     queryWrapper.eq("schoolId" ,userEducational.getSchoolid());
}
    if(userEducational.getUserId() != null){
     queryWrapper.eq("user_id" ,userEducational.getUserId());
}
            List<UserEducational> list = userEducationalService.list(queryWrapper);
        return getDataTable(list);
    }


    @Autowired
    ITbSchoolService schoolService;


    @GetMapping("/getSchool")
    public List<String> getSchool(){
     return schoolService.findName();
    }

    @GetMapping("/appletList" )
    public TableDataInfo appletList(String openId) {
        List<EducationSchoolVo> list = userEducationalService.findByopenId(openId);
        Iterator iterator = list.iterator();
        List<RestVo> restVos = new ArrayList<>();
        while (iterator.hasNext()){
            EducationSchoolVo technology =  (EducationSchoolVo)iterator.next();
            RestVo restVo = (RestVo)technology.clone();
            restVos.add(restVo);
        }
        return getDataTable(restVos);
    }
    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:educational:export')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(UserEducational userEducational) {
        QueryWrapper<UserEducational> queryWrapper = new QueryWrapper<UserEducational>(userEducational);
        List<UserEducational> list = userEducationalService.list(queryWrapper);
        ExcelUtil<UserEducational> util = new ExcelUtil<UserEducational>(UserEducational. class);
        return util.exportExcel(list, "educational" );
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:educational:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(userEducationalService.findById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:educational:add')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserEducational userEducational) {
        return toAjax(userEducationalService.save(userEducational) ? 1 : 0);
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:educational:edit')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserEducational userEducational) {
        return toAjax(userEducationalService.updateById(userEducational) ? 1 : 0);
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:educational:remove')" )
    @Log(title = "【请填写功能名称】" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userEducationalService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }


}
